<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单结算城市、配送站、配送员联动ajax类
 * @package  ajax/comm/bill
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysRegion.class.php');
quanli_require('module/base/system/sysDistribution.class.php');
quanli_require('module/base/system/sysDistributionTechnician.class.php');

class ajax_distribution {


    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {

                case 1: //查询城市
                    $this->searchCity();
                    break;

                case 2: //根据用户权限查询配送站
                    $this->searchDistribution();
                    break;

                case 3: //查询配送员
                    $this->searchMarki();
                    break;

                case 4: //查询配送站
                    $this->searchAllDistribution();
                    break;

                case 5: //查询地区
                    $this->searchArea();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    /**
     * 查询城市
     */
    private function searchCity() {

        global $G_SHOP;

        $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');
        $city_array = array();
        $get_city_array = $G_SHOP->getCache('city');
        if($get_city_array){
            foreach($get_city_array as $city){
                if(in_array($city['id'], $PS_LOGIN_SESSION['city'])){
                    $city_array[] = $city;
                }
            }
        }

        returnJsonData('ok',200,$city_array);

    }

    /*
     * 根据用户权限所属城市查找权限所有配送站
     */
    public function searchDistribution(){

        global $G_SHOP;

        //判断会员号
        if (!isset($_REQUEST['deliver_cityid']) || empty($_REQUEST['deliver_cityid'])) {
            returnJsonData('参数错误');
        }

        $_REQUEST['cityid'] = $_REQUEST['deliver_cityid'];

        //实例化配送网点设置类
        $sysDistribution = new sysDistribution( ) ;

        $arg_array = $_REQUEST;
        $arg_array['useFlg'] = 1;

        $return_array = $sysDistribution->search( $arg_array ) ;
        if (isset($_REQUEST['no_act'])) {
            returnJsonData('ok', 200, $return_array);
        }

        $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');
        $distribution_array = array();
        if($return_array){
            foreach($return_array as $distribution){
                if(in_array($distribution['distribution_id'], $PS_LOGIN_SESSION['distribution'])){
                    $distribution_array[] = $distribution;
                }
            }
        }

        returnJsonData('ok',200,$distribution_array);
    }

    /*
     * 根据城市查找所有配送站
     */
    public function searchAllDistribution(){

        global $G_SHOP;

        //判断会员号
        if (!isset($_REQUEST['deliver_cityid']) || empty($_REQUEST['deliver_cityid'])) {
            returnJsonData('参数错误');
        }

        $_REQUEST['cityid'] = $_REQUEST['deliver_cityid'];

        //实例化配送网点设置类
        $sysDistribution = new sysDistribution( ) ;

        $arg_array = $_REQUEST;
        $arg_array['useFlg'] = 1;

        $return_array = $sysDistribution->search( $arg_array ) ;

        returnJsonData('ok',200,$return_array);
    }

    /*
     * 根据配送站查找配送员
     */
    private function searchMarki(){

        if(!isset($_REQUEST['distribution_id']) || empty($_REQUEST['distribution_id'])){
            returnJsonData('参数错误');
        }
        //实例化配送员类
        $sysDistributionTechnician = new sysDistributionTechnician();

        $arg_array = $_REQUEST;
        $arg_array['useFlg'] = 1;

        $return_array = $sysDistributionTechnician->search( $arg_array );

        returnJsonData('ok',200,$return_array);

    }

    /*
    * 根据用户权限所属城市查找权限所有地区
    */
    public function searchArea(){

        global $G_SHOP;

        //判断会员号
        if (!isset($_REQUEST['deliver_cityid']) || empty($_REQUEST['deliver_cityid'])) {
            returnJsonData('参数错误');
        }

        $_REQUEST['cityid'] = $_REQUEST['deliver_cityid'];

        //实例化配送网点设置类
        $sql = "SELECT * FROM sys_region WHERE parentID = " . $_REQUEST['cityid'];
        $return_array = $G_SHOP->DBCA->stmt_query($sql);

        $area_array = array();
        if($return_array){
            foreach($return_array as $key => $area){
                $area_array[$key]['id'] = $area->id;
                $area_array[$key]['name'] = $area->name;
                $area_array[$key]['parentID'] = $area->parentID;
                }
            }

        returnJsonData('ok',200,$area_array);
    }

}